package jannex.label;

import jannex.internal.BaseIterator;

import java.util.Iterator;
import java.util.NoSuchElementException;

class SingletonLabelSet extends LabelSet {

    private final String label;

    SingletonLabelSet(String label) {
        this.label = label;
    }

    @Override
    public Iterator<String> iterator() {
        return new BaseIterator<String>() {
            boolean once;

            @Override
            public boolean hasNext() {
                return !once;
            }

            @Override
            public String next() {
                if (!hasNext()) {
                    throw new NoSuchElementException();
                }
                once = true;
                return label;
            }
        };
    }

    @Override
    public int size() {
        return 1;
    }

    @Override
    public boolean contains(Object o) {
        if (o == null) {
            return label == null;
        }
        return o instanceof String && label.equalsIgnoreCase(o.toString());
    }

    @Override
    public LabelList asList() {
        return new SingletonLabelList(label);
    }

    @Override
    public String[] toArray() {
        return new String[]{label};
    }
}
